/**
 * 接口受权管理初始化
 */
var AppRoleResource = {
    id: "AppRoleResourceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    roleTree:null,
    appId:appInstanceId,
    roleId:null
};

/**
 * 初始化表格的列
 */
AppRoleResource.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter:function(value,row,index){
        	if (row.check == true)
            return {
                checked : true//设置选中
            };
            return value;
        }},
        {title: '主键id', field: 'id',visible: false,  align: 'center', valign: 'middle', sortable: true},
        {title: '服务id', field: 'aid',visible: false,  align: 'center', valign: 'middle', sortable: true},
        {title: '接口编码', field: 'resourceCode', align: 'center', valign: 'middle', sortable: true},
        {title: '接口名称', field: 'resourceName', align: 'center', valign: 'middle', sortable: true},
        {title: '接口分组', field: 'resourceGroup', align: 'center', valign: 'middle', sortable: true},
        {title: '接口类型', field: 'resourceTypeName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='数据服务'){
                return "<span class='label label-success'>数据服务</span>";
        	}else{
                return "<span class='label label-info'>页面元素</span>";
        	}
        }},
        {title: '排序', field: 'weight', align: 'center', valign: 'middle', sortable: true},
	];
};

/**
 * 检查是否选中
 */
AppRoleResource.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
    	var items =""
    	var _index = 0;
    	$.each(selected, function(i, o) {
    		if(_index==0){
    			items += o.id;
    			_index = 1;
    		}else{
    			items += ";"+o.id;
    		}
		});
        AppRoleResource.seItem = items;
        return true;
    }
};

/**
 * 保存接口受权
 */
AppRoleResource.save = function () {
	if(AppRoleResource.check()){
	    var queryData = {};
	    queryData['roleId'] = AppRoleResource.roleId;
	    queryData['appId'] = AppRoleResource.appId;
	    queryData['resourceIds'] = AppRoleResource.seItem;
	    //提交信息
	    var ajax = new $ax(Sys.backendPath + "/approleresource/save", function(data){
	        Sys.success("受权成功!");
	    },function(data){
	        Sys.error("受权失败!" + data.responseJSON.message + "!");
	    });
	    ajax.set(queryData);
	    ajax.start();
	}else{
        Sys.info("请先选中受权接口！");
	}
};

/**
 * 点击角色树
 * 
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
AppRoleResource.onClickRoleTreeNode = function(e, treeId, treeNode) {
	AppRoleResource.roleId = treeNode.id>0?treeNode.id:null;
	AppRoleResource.loadResource();
}


/**
 * 加载接口数据
 * 
 * @returns
 */
AppRoleResource.loadResource = function() {
	if(AppRoleResource.appId!=null&&AppRoleResource.roleId!=null){
		$("#btn_save").show();
		if(AppRoleResource.table==null){
		    var defaultColunms = AppRoleResource.initColumn();
		    var table = new BSTable(AppRoleResource.id, Sys.backendPath + "/approleresource/list", defaultColunms);
		    var queryData = {};
		    queryData['appId'] = AppRoleResource.appId;
		    queryData['roleId'] = AppRoleResource.roleId;
		    table.setQueryParams(queryData);
		    table.setPaginationType("client");
		    table.setShowColumns(false);
		    table.setShowRefresh(false);
		    table.setSearch(true);
		    table.setSortName("resourceWeight");
		    AppRoleResource.table = table.init();
		}else{
		    var queryData = {};
		    queryData['appId'] = AppRoleResource.appId;
		    queryData['roleId'] = AppRoleResource.roleId;
		    AppRoleResource.table.refresh({query: queryData});
		}
	}
}

$(function () {
	$("#btn_save").hide();
	var roleTree = new $ZTree("roleTree", Sys.backendPath + "/role/roleTree/user");
	roleTree.bindOnClick(AppRoleResource.onClickRoleTreeNode);
	roleTree.bindOnAsyncSuccess(function(event, treeId, treeNode, msg){
		var nodes = roleTree.getObject().getNodes();
		if(treeNode!=null){
			nodes = treeNode.children;
		}
		for (var i=0, l=nodes.length; i<l; i++) {  
			roleTree.getObject().expandNode(nodes[i], true, false, false);//展开节点就会调用后台查询子节点  
        } 
	})
	roleTree.initAsync(["id"]);
	AppRoleResource.roleTree = roleTree;

});
